package controller;

import common.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import pojo.SysSchedule;
import service.SysScheduleService;
import service.impl.SysScheduleServiceImpl;
import util.WebUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ： CodeWater
 * @create ：2022-04-05-16:05
 * @Function Description ：
 *
 * 1.定义控制器类
 * 2.定义控制器类方法
 * 3.定义控制器类方法参数
 * 4.定义控制器类方法返回值
 * 5.定义控制器类方法处理逻辑
 */
@WebServlet("/schedule/*")
public class SysScheduleController extends BaseController {
    private SysScheduleService scheduleService = new SysScheduleServiceImpl();
    /**
     * 添加日程
     * @param req
     * @param resp
     */
   protected void addDefaultSchedule(HttpServletRequest req, HttpServletResponse resp){
       Integer uid = Integer.parseInt(req.getParameter("uid"));
       int count = scheduleService.addDefaultSchedule(uid);
       WebUtil.writeValue(resp,Result.ok(null));
   }
   /**
    * 查询所有日程
    * @param req
    * @param resp
    */
   protected void findAllSchedule(HttpServletRequest req, HttpServletResponse resp){
       //接收请求
       Integer uid = Integer.parseInt(req.getParameter("uid"));
       // 查询用户的所所有日程，
       List<SysSchedule> list = scheduleService.findAllScheduleByUid(uid);
       // 将日程放入一个result对象，
       Map map = new HashMap();
       map.put("itemlist",list);
       Result ok = Result.ok(map);
       // 转换为JSON串，响应给前端
       WebUtil.writeValue(resp,ok);

   }
   /**
    * 修改日程
    * @param req
    * @param resp
    */
   protected  void updateSchedule(HttpServletRequest req, HttpServletResponse resp){
       SysSchedule sysSchedule = WebUtil.readJson(req, SysSchedule.class);
       int count = scheduleService.updateSchedule(sysSchedule);
       WebUtil.writeValue(resp,Result.ok(null));

   }
   /**
    * 删除日程
    * @param req
    * @param resp
    */
   protected void removeSchedule(HttpServletRequest req, HttpServletResponse resp){
       Integer sid = Integer.parseInt(req.getParameter("sid"));
       int count = scheduleService.removeSchedule(sid);
       WebUtil.writeValue(resp,Result.ok(null));
   }

}
